package com.ht.mapper;

import com.ht.entity.SysUserEntity;
import com.ht.model.vo.SysUserVO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class SysUserMapper {
    /**
     * 单个实体类转换成vo类
     * @param entity 实体类
     * @return vo类
     */
    //实力护转vo
    public static SysUserVO entity2VO(SysUserEntity entity){
        //实例化DAO
        SysUserVO vo = new SysUserVO();
        //设置属性
        vo.setUserId(entity.getUserId());
        vo.setUsername(entity.getUsername());
        vo.setStatus(entity.getStatus()==1 ? "启用" :"禁用");
        vo.setPhone(entity.getPhone());
        vo.setRealname(entity.getRealname());
        vo.setGender(entity.getGender() == 1 ? "男" : "女");
        vo.setCreateBy(entity.getCreateBy());
        vo.setCreateTime(entity.getCreateTime());
        vo.setUpdateBy(entity.getUpdateBy());
        vo.setUpdateTime(entity.getUpdateTime());
        return vo;
    }

    /**
     * 实体类集合转换成vo类集合
     * @param list 实例类集合
     * @return vo集合
     */
    public static  List<SysUserVO> entity2VO(List<SysUserEntity> list){
        List<SysUserVO> voList = new ArrayList<SysUserVO>();
        list.forEach(entity -> voList.add(entity2VO(entity)));
        return voList;
    }

    /**
     * 结果集转实体类
     * @param resultSet 结果集
     * @return 返回实体类
     */
    public static SysUserEntity toEntity(ResultSet resultSet) throws SQLException {
        SysUserEntity entity = new SysUserEntity();
           if(resultSet.next()){
               entity.setUserId(resultSet.getLong("userId"));
               entity.setUsername(resultSet.getString("username"));
               entity.setPassword(resultSet.getString("password"));
               entity.setSalt(resultSet.getString("salt"));
               entity.setStatus(resultSet.getInt("status"));
               entity.setPhone(resultSet.getString("phone"));
               entity.setRealname(resultSet.getString("realname"));
               entity.setGender(resultSet.getInt("gender"));
               entity.setCreateBy(resultSet.getString("createby"));
               entity.setCreateTime(resultSet.getDate("createtime"));
               entity.setUpdateBy(resultSet.getString("updateby"));
               entity.setUpdateTime(resultSet.getDate("updatetime"));
               return entity;
           }else{
               return null;
           }
        }

    /**
     * 结果集转换成实体类集合
     * @param resultSet 结果集
     * @return 实体类集合
     * @throws SQLException SQL异常
     */
    public static List<SysUserEntity> toEntities(ResultSet resultSet) throws SQLException {
        List<SysUserEntity> list = new ArrayList<>();
        while (resultSet.next()){
            SysUserEntity entity = new SysUserEntity();
            entity.setUserId(resultSet.getLong("userId"));
            entity.setUsername(resultSet.getString("username"));
            entity.setPassword(resultSet.getString("password"));
            entity.setSalt(resultSet.getString("salt"));
            entity.setStatus(resultSet.getInt("status"));
            entity.setPhone(resultSet.getString("phone"));
            entity.setRealname(resultSet.getString("realname"));
            entity.setGender(resultSet.getInt("gender"));
            entity.setCreateBy(resultSet.getString("createby"));
            entity.setCreateTime(resultSet.getDate("createtime"));
            entity.setUpdateBy(resultSet.getString("updateby"));
            entity.setUpdateTime(resultSet.getDate("updatetime"));
            list.add(entity);
        }
        return list;
    }
}
